/**
 * name: Req.js
 * description: request处理基础类
 * author: 王希平
 * date: 2018-11-04
 */
import { PARAMSESSION, URL} from './config'

class Req {
  
/**
  * 设置请求头
  */
  _header () {
    return this._header = PARAMSESSION
  }

/**
  * 设置统一的异常处理
  */
  _setErrorHandler(statusCode) {
    switch (statusCode) {
      case 401:
        wx.showToast({
          title: '登录过期请重新登入',
          icon: 'none',
          duration: 2000
        })
        break
      case 404:
        wx.showToast({
          title: '接口不存在',
          icon: 'none',
          duration: 2000
        })
        break
      case 500:
        wx.showToast({
          title: '服务器异常',
          icon: 'none',
          duration: 2000
        })
        break
      case 504:
        wx.showToast({
          title: '网络超时',
          icon: 'none',
          duration: 2000
        })
        break
      default:
        Message.warning({
          content: '服务器繁忙，请稍候再试'
        })
    }
  }


/**
 * GET类型的网络请求
 */
  static getRequest(url, data, header = new Req()._header()) { 
    return Req.requestAll(url, data, header, 'GET')
  }


/**
  * DELETE类型的网络请求
  */
  static deleteRequest(url, data, header = new Req()._header()) {
    return Req.requestAll(url, data, header, 'DELETE')
  }

  /**
   * PUT类型的网络请求
   */
  static putRequest(url, data, header = new Req()._header()) {
    return Req.requestAll(url, data, header, 'PUT')
  }


/**
  * POST类型的网络请求
  */
  static postRequest(url, data, header = new Req()._header()) {
    return Req.requestAll(url, data, header, 'POST')
  }

  /**
   * 网络请求
   */
  static requestAll(url, data, header, method) {
    return new Promise((resolve, reject) => {
      wx.showNavigationBarLoading()
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: URL.baseUrl + url+'?_t=' + new Date().getTime(),
        data: data,
        header: header[0],
        method: method,
        success: (res => {
          wx.hideLoading()
          if (res.statusCode === 200) {
            //200: 服务端业务处理正常结束
            wx.hideLoading()    
            resolve(res.data)
          } else {
              //如果有统一的异常处理，就先调用统一异常处理函数对异常进行处理   
              wx.hideLoading()           
              new Req()._setErrorHandler(res.statusCode)
              reject(res)
            }
        }),
        fail: (res => {    
          wx.hideLoading()    
          new Req()._setErrorHandler(res.statusCode)       
          reject(res)
        })
      })
    })    
  }
  
  /**
   * 上传文件
   */
  static uploadFile(url, path) {
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    })
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: URL.uploadUrl + url,
        filePath: path[0],
        name: 'file',
        header: new Req()._header()[1],
        formData: {
          //和服务器约定的token, 一般也可以放在header中
          'session_token': wx.getStorageSync('session_token')
        },
        success: function (res) {
          if (res.statusCode == 200) {            
            resolve(res.data)
          } else {
            wx.hideLoading()
            //如果有统一的异常处理，就先调用统一异常处理函数对异常进行处理   
            new Req()._setErrorHandler(res.statusCode)
          }
        },
        fail: function (res) {        
          new Req()._setErrorHandler(res.statusCode) 
          reject(res) 
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })  
    })    
  }
}

export default Req
